package com.crm.sys.tab;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.crm.sys.model.SysTerm;
import com.crm.sys.service.SysService;
import com.crm.util.Constants;
/**
 *  配置表标签
 * @author Administrator
 *
 */
@Repository
public class MenuTag extends RequestContextAwareTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3258620188746752599L;
     
    private String value; //传入的默认值 
    private Long uid;
    
	public void setUid(Long uid) {
		this.uid = uid;
	}

	public void setValue(String value) {
		this.value = value;
	}
 
	public MenuTag(){  
    }  
      
	@Override
	protected int doStartTagInternal() throws Exception {  
        JspWriter out = this.pageContext.getOut();  
          
        try {  
        	SysService sysService  = (SysService)this.getRequestContext().getWebApplicationContext().getBean("sysService");  
         
        	  try{
        		  String outString ="";
        		
        			  
        			 
        				   // 查询service   
        				/* Constants.MENULIST  =  sysService.getMune(uid);
        			 
        		 out.print(getSelectHtml(Constants.MENULIST, value));*/  
        		  out.print("");  
		       	}catch(Exception e1){
		       	 out.print("");  
		       	}
           
        } catch (IOException e) {  
            //e.printStackTrace();  
        }  
          
      
        return EVAL_BODY_INCLUDE;  
    }   
	 
	
	/**
	 * 
	 * @param list
	 * @param value   默认值
	 * @return
	 * @throws Exception
	 */
	private String getSelectHtml(List<SysTerm> list,String value) throws Exception{
		  StringBuffer  s = new StringBuffer();
		  String subHtml = " <li><a href='{0}'>{1}</a></li>";
		  for(SysTerm item:list){
			  //判断是否有权限显示菜单
			  if(item.getChecked()==1){
				  s.append("<li class='");
				  boolean isSubMenu= false; //标示是否有子菜单
				   if(item.getSubList()!=null&&item.getSubList().size()>0){
					   //有子菜单
					   s.append("dropdown' ><a data-toggle='dropdown'") ;
					   isSubMenu= true;
				   }else{
					   //无子菜单 
					     s.append("'><a "); 
				   }
				   if(item.getId() !=null && item.getId()==Integer.parseInt(value)){
					   s.append("class=\"current\"");
				   }
				   s.append(" href='").append(item.getUrl()).append("'>").append(item.getName());
				   if(isSubMenu){
					   s.append("<b class='caret'></b></a><ul class='dropdown-menu'>");
					  try{
						  for(SysTerm sitem:item.getSubList()){
							  try{
								  if(sitem.getChecked()==1){
									  //循环生产二级菜单
									  
									   if(sitem.getSubList()!=null&&sitem.getSubList().size()>0){
										   try{
											   s.append("<li class='dropdown-submenu'><a href='javascript:;'>");
											   s.append(sitem.getName());
											   s.append("</a> <ul class='dropdown-menu'>");
												  for(SysTerm titem:sitem.getSubList()){
													   try{
														   if(titem.getChecked()==1){
															   s.append(subHtml.replace("{0}", titem.getUrl()).replace("{1}", titem.getName()));
															 }
													   }catch(Exception e){
														   
													   }
												  }
												  s.append("</ul></li>");		  
											} catch(Exception e){
											   
										   }
										     
											/*<li class="dropdown-submenu"><a href="#"> 
											 Editable DataTables  
											</a> 
											
											    <ul class="dropdown-menu">
							                        <li><a href="javascript:;">二级菜单</a></li>
							                        <li class="divider"></li>
							                        <li class="dropdown-submenu">
							                            <a href="javascript:;">二级菜单</a>
							                            <ul class="dropdown-menu">
							                                <li><a href="javascript:;">三级菜单</a></li>
							                            </ul>
							                        </li>
							                    </ul>
															
											</li>*/
									   }else{
										   s.append(subHtml.replace("{0}", sitem.getUrl()).replace("{1}", sitem.getName()));
									   }
									  
								  }
							  }catch(Exception e){
								  e.printStackTrace();
							  }
						   }
					  }catch(Exception e){
						  e.printStackTrace();
					  }
					   s.append("</ul>");
				   }else{
					   s.append("</a>");
				   } 
				   s.append("</li>");
			  }
			  
			 
			 
		  }
		 return s.toString();
	}
	
 
    public int doAfterBody() throws JspException{  
       // System.out.println("处理标签体（after body）....");  
        return SKIP_BODY;  
    }  
    public int doEndTag() throws JspException{  
      //  System.out.println("对象正在处理结束标记.....");  
        return EVAL_PAGE;  
    }  
}
